package com.sohoometer.assignmeter.dao;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.nari.coreframe.dao.hibernate3.GenericDaoImpl;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;
import com.sohoometer.assignmeter.bean.AssignMeterDataExceptionResult;

@Repository("assignMeterDataExceptionResultDao")
public class AssignMeterDataExceptionResultDaoImpl  extends GenericDaoImpl<AssignMeterDataExceptionResult, String> implements AssignMeterDataExceptionResultDao{ 
	
	@Autowired
	private JdbcTemplate  jdbcTemplate = null;
	@Autowired
	public AssignMeterDataExceptionResultDaoImpl(SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}
	
	@Override
	public  ResultWithPageCond<AssignMeterDataExceptionResult> queryResultByQueryAndExceptionType(String queryId, int exceptionType,PageCond pageCond){
		if(StringUtils.isBlank(queryId)) return null;
		if(exceptionType<1) return null;
		String sql = " select * from p_assignmeter_exception_result   where queryid=\'"+queryId+"\' and exceptionType&"+exceptionType+">0 ";
		String countsql = " select count(id)  from p_assignmeter_exception_result   where queryid=\'"+queryId+"\' and exceptionType&"+exceptionType+">0";
		if(pageCond!=null) {
			 sql+= " LIMIT "+pageCond.getStart()+", "+pageCond.getLimit()+";";
		}
		List<AssignMeterDataExceptionResult> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<AssignMeterDataExceptionResult>(AssignMeterDataExceptionResult.class));
		Integer total = jdbcTemplate.queryForObject(countsql, Integer.class);
		pageCond.setTotal(total);
		ResultWithPageCond<AssignMeterDataExceptionResult> resultWithPageCond = new ResultWithPageCond<AssignMeterDataExceptionResult>();
		resultWithPageCond.setPageCond(pageCond);
		resultWithPageCond.setList(list);
		return resultWithPageCond;
	}
	
	/**
	 * 根据查询id来删除查询Id所关联的所有结果
	 * */
	@Override
	public void deleteResultByQueryId(String queryId) {
		String sql = "DELETE FROM  p_assignmeter_exception_result  where  queryid = ?";
		 jdbcTemplate.update(sql, queryId);
	}
	
}
